/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.core.ast;

import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
import org.eclipse.dltk.ast.declarations.TypeDeclaration;
import org.eclipse.dltk.compiler.util.Util;


public class USModuleDeclaration extends ModuleDeclaration implements USIScope {

	private String name;
	private String packageName;

	public static String getModuleNameFromFileName(final String fileName) {
		int indexOfModuleName = fileName.lastIndexOf('/');
		int indexOfModuleNameEnd = fileName.lastIndexOf('.');
		String moduleName = Util.EMPTY_STRING;
		if (indexOfModuleName > 0 && indexOfModuleNameEnd > 0) {
			moduleName = fileName.substring(indexOfModuleName + 1,
					indexOfModuleNameEnd);
		}
		return moduleName;
	}

	public USModuleDeclaration(int sourceLength) {
		super(sourceLength);
	}

	public USModuleDeclaration(int sourceLength, boolean rebuildEnabled) {
		super(sourceLength, rebuildEnabled);
	}

	public USModuleDeclaration() {
		super(0);
	}

	public void setClassDeclaration(USClassDeclaration usClassDeclaration) {
		if (usClassDeclaration != null)
			addStatement(usClassDeclaration);
	}

	@Override
	public String toString() {
		return super.toString();
	}

	public void setName(String moduleName) {
		this.name = moduleName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public String getName() {
		return name;
	}

	public String getPackageName() {
		return packageName;
	}

	public USClassDeclaration getClassDeclaration() {
		TypeDeclaration[] types = this.getTypes();
		for (TypeDeclaration typeDeclaration : types) {
			if (typeDeclaration instanceof USClassDeclaration)
				return (USClassDeclaration) typeDeclaration;
		}
		return null;
	}
}
